package cn.edu.zut.internship.service.impl.notify;

import cn.edu.zut.internship.bean.sys.Result;
import cn.edu.zut.internship.bean.sys.SysUser;
import cn.edu.zut.internship.iservice.notify.ChatSV;
import cn.edu.zut.internship.iservice.notify.NotifySV;
import cn.edu.zut.internship.mapper.notify.ChatMapper;
import cn.edu.zut.internship.mapper.notify.NotifyMapper;
import cn.edu.zut.internship.utils.PrimaryKeyUtil;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class ChatSVImpl implements ChatSV {
    @Autowired
    ChatMapper chatMapper;
    @Autowired
    PrimaryKeyUtil primaryKeyUtil;
    @Autowired
    NotifySV notifySV;
    @Autowired
    NotifyMapper notifyMapper;
    @Override
    public Result getChatList(Map params) {
        Result result = new Result();
        List<Map> chatList = chatMapper.getChatList(params);
        result.setBeans(chatList);
        return result;
    }

    @Override
    public Result sendChat(Map params) {
        Result result = new Result();
        SysUser userInfo = (SysUser) params.get("userInfo");
        //发送聊天信息
        Long chatId = primaryKeyUtil.generatePrimaryKey("chat");
        params.put("chatId",chatId);
        chatMapper.sendChat(params);
        //判断聊天信息接收者消息中心是否存在提醒，若存在更新提醒，若不存在新建提醒消息
        List<Map> maps = notifyMapper.selectChatNotify(params);
        if(CollectionUtils.isEmpty(maps)){//不存在提醒消息
            Map notify = new HashMap();
            notify.put("sendAccount",params.get("sendAccount"));
            notify.put("sendType",0);
            notify.put("notifyType",3);
            notify.put("internshipId",params.get("internshipId"));
            notify.put("content",userInfo.getUserName()+"给你发送一条私信："+params.get("content"));
            notify.put("targetAccount",params.get("targetAccount"));
            notify.put("targetType",0);
            notify.put("creatAccount",params.get("sendAccount"));
            notifySV.addNotify(notify);
        }else{//存在提醒消息
            //更新消息内容
            Map notify = notifyMapper.getNotify((Long) maps.get(0).get("notifyId"));
            notify.put("readeType",0);
            notify.put("content",userInfo.getUserName()+"给你发送一条私信："+params.get("content"));
            notifyMapper.updateNotify(notify);
        }
        return result;
    }
}
